package com.zq.designpaterns.create.prototype;

import java.util.Hashtable;
import java.util.Objects;

/**
 * ================================================
 * 作    者：程志强
 * 邮    箱：cheng.zhiqiang@cesgroup.com.cn
 * 版    本：1.0.0
 * 创建日期：2021/01/11  17:19
 * 修改日期：
 * 描    述：
 * 修订历史：
 * ================================================
 */
public class ShapeCache {
    private static final Hashtable<String, Shape> shapeMap = new Hashtable<>();

    public static Shape getShape(String shapeId) throws CloneNotSupportedException {
        Shape shape = shapeMap.get(shapeId);
        if (Objects.isNull(shape)) {
            return null;
        }
        return (Shape) shape.clone();
    }

    public static void loadCache() {
        Circle circle = new Circle();
        circle.setId("1");
        shapeMap.put(circle.getId(), circle);

        Square square = new Square();
        square.setId("2");
        shapeMap.put(square.getId(), square);

        Rectangle rectangle = new Rectangle();
        rectangle.setId("3");
        shapeMap.put(rectangle.getId(), rectangle);
    }
}
